<template>
  <view class="order-list">
    <tabs :tabsList="['全部','待签约','待提车','交易完成','交易取消']" @itemClick="itemClick"></tabs>
    <order :orderList="orderList" @cancelOrder="cancelOrder" @completeOrder="completeOrder" @deleteOrder="deleteOrder"></order>
		<empty text="您还没有相关订单~" v-show="!orderList.length">
				<u-button type="primary" text="去看看~" size="small"></u-button>
		</empty>
		<u-loadmore :status="loadingText.status" :loading-text="loadingText.inLoading"
			:loadmore-text="loadingText.beforeLoading" :nomore-text="loadingText.nomoreText" :height="40" v-show='orderList.length>=order_params.limit' />
  </view>
</template>

<script>
	import {get_order,upd_order_state} from '@/api/contents/order/order.js'
	
  import tabs from '@/components/profile/orderList/tabs/tabs.vue'
  import order from '@/components/profile/orderList/order/order.vue'
	import empty from '@/components/home/empty/empty.vue'
  export default {
    components:{
      tabs,
      order,
			empty
    },
    data() {
      return {
        order_params:{
					state:0,
					limit:3,
					page:1,
				},
				orderList:[],
				loadingText: {
					inLoading: '努力加载中',
					beforeLoading: '上拉加载更多',
					nomoreText: '没有更多了',
					status: 'loadmore'
				}
      };
    },
		methods:{
			itemClick(curr){
				this.order_params.state=curr;
				this.orderList=[];
				this.order_params.page=1;
				this.getOrder();
			},
			// 获取订单
			getOrder(){
				get_order(this.order_params).then(res=>{
					if (res.code == 1) {
						if (res.data.length) {
							this.orderList = [...this.orderList, ...res.data]
							this.loadingText.status = 'loadmore';
						} else {
							this.loadingText.status = 'nomore'
						}
					}
				})
			},
			// 取消订单
			cancelOrder(order_id){
				upd_order_state({
					order_id:order_id,
					state:4
				}).then(res=>{
					if(res.code==1){
						this.orderList=[];
						this.order_params.page=1;
						this.getOrder();
						uni.showToast({
						  icon:'none',
						  title:'你取消了订单！'
						})
					}
				})
			},
			// 完成订单
			completeOrder(order_id){
				upd_order_state({
					order_id:order_id,
					state:3
				}).then(res=>{
					if(res.code==1){
						this.orderList=[];
						this.order_params.page=1;
						this.getOrder();
						uni.showToast({
						  icon:'none',
						  title:'你完成了订单！'
						})
					}
				})
			},
			// 删除订单
			deleteOrder(order_id){
				upd_order_state({
					order_id:order_id,
					state:0
				}).then(res=>{
					if(res.code==1){
						this.orderList=[];
						this.order_params.page=1;
						this.getOrder();
						uni.showToast({
						  icon:'none',
						  title:'你删除了订单！'
						})
					}
				})
			}
		},
		onShow() {
			this.order_params.page=1;
			this.orderList=[];
			this.getOrder();
		},
		onReachBottom() {
			this.loadingText.status = 'loading';
			this.order_params.page++;
			this.getOrder();
		},
  }
</script>

<style lang="scss" scoped>
  .order-list{
    // #ifdef H5
    height: calc(100vh - 44px);
    //  #endif
    // #ifdef MP-WEIXIN
    height: 100vh;
    // #endif
    background-color: #F5F7FA;
    padding: 0 20upx;
  }
</style>
